package com.lebang.medicalInquiry.component;

import com.alibaba.fastjson.JSONObject;


import com.lebang.medicalInquiry.mapper.SysUserMapper;
import com.lebang.medicalInquiry.model.po.SysUser;
import com.lebang.medicalInquiry.utils.AESUtil;
import com.lebang.medicalInquiry.utils.TextUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zpc
 * @version V1.0
 * @date
 */
@Component
public class TokenComponent {

    @Resource
    private SysUserMapper sysUserMapper;

    @Value("${aes.secret}")
    private String secret;


    /**
     * 生成用户的token
     *
     * @param token
     * @return
     */
    public SysUser getByToken(String token) {
        try {
            if (!TextUtil.isEmpty(token)) {
                String data = AESUtil.decryptHex(token, secret);
                return JSONObject.parseObject(data, SysUser.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成用户的token
     *
     * @param sysUser
     * @return
     */
    public String getByUser(SysUser sysUser) {
        try {
            if (!TextUtil.isEmpty(sysUser)) {
                String s = JSONObject.toJSONString(sysUser);
                return AESUtil.encryptHex(s, secret);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
